package com.niuwa.p2p.action.claimtransfer;

import java.math.BigDecimal;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import com.niuwa.p2p.action.JsonAction;
import com.niuwa.p2p.entity.claimtransfer.PlanTransfer;
import com.niuwa.p2p.entity.productTemplate.ProductProtocol;
import com.niuwa.p2p.entity.user.User;
import com.niuwa.p2p.service.claimtransfer.PlanTransferService;
import com.niuwa.p2p.vo.business.InvestPlanProductVo;
import com.niuwa.p2p.vo.claimtransfer.WaitMatchVo;

/**
 * 投资计划债权转让
 * @author money
 *
 */
@Namespace("/claimtransfer")
@Controller
public class PlanTransferAction  extends JsonAction{
	private static final long serialVersionUID = 1L;
	
	@Autowired
	private PlanTransferService planTransferService;
	
	/**
	 * 跳转到列表界面
	 * @return
	 */
	@Action("gotoPlanTransferList")
	public String gotoCustomerList(){
		return "plantransferlist";
	}
	
	/**
	 * 获取列表数据
	 * @return
	 */
	@Action("getPlanTransferList")
	public String getPlanTransferList(){
		setObjectReSult(planTransferService.searchPlanTransferPage(getParamMap()));
		return NONE;
	}
	
	/**
	 * 查看记录详情
	 * @return
	 */
	@Action("viewPlanTransferDetail")
	public String viewPlanTransferDetail(){
		InvestPlanProductVo in = planTransferService.viewPlanProDetail(getParam("prodCode"));
		this.getRequest().setAttribute("planTransfer", in);
		this.getRequest().setAttribute("recordNo", getParam("recordNo"));
		this.getRequest().setAttribute("custLoginName", getParam("custLoginName"));
		this.getRequest().setAttribute("custId", getParam("custId"));
		this.getRequest().setAttribute("remainAmount", getParam("remainAmount"));
		return "planTransferDetail";
	}
	/**
	 * 获取产品相关协议
	 */
	@Action("getProcolByproductCode")
	public String getProcolByProductCode(){
		List<ProductProtocol> list = planTransferService.getProductProtocol(getParam("prodCode"));
		//this.setResult(true, null, list);
		this.getRequest().setAttribute("list", list);
		return "protocolList";
	}
	/**
	 * 获取带匹配资金和待转让资金
	 * @return
	 */
	@Action("getWaitMatch")
	public String getWaitMatch(){
		BigDecimal waitMatchFund = planTransferService.getWaitMatchFund();
		BigDecimal waitMatchClaimFund = planTransferService.getWaitClaimMatchFund();
		WaitMatchVo wmv = new WaitMatchVo(waitMatchFund, waitMatchClaimFund);
		setResult(true, null,wmv);
		return 	NONE;
	}
	
	/**
	 * 获取投资计划债权转让详细页列表
	 * @return
	 */
	@Action("getPlanTransferDetailList")
	public String getPlanTransferDetailList(){
		this.setObjectReSult(planTransferService.searchPlanTransferApplyList(getParam("recordNo")));
		return NONE;
	}
	/**
	 * 投资计划债权转让列表审核拒绝
	 * @return
	 */
	@Action("planTransferReject")
	public String planTransferReject(){
		//判断该条记录是否已经审核过
		PlanTransfer pt = planTransferService.getByRecordNo(getParam("recordNo"));
		if(pt != null){
			String status = pt.getStatus();
			if(!"WAIT_AUDIT".equals(status)){//记录已经审核过
				this.setResult(true, "该条记录已经审核过", null);
				return NONE;
			}
		}
		Long userId = ((User)this.getRequest().getSession().getAttribute("user")).getId();
		boolean result = planTransferService.saveAduitReject(getParam("recordNo"), userId, getParam("remark"));
		if(result){
			this.setResult(true, "操作成功!", null);
		}else{
			this.setResult(false, "操作失败!", null);
		}
		return NONE;
	}
	/**
	 * 投资计划债权转让列表审核通过
	 * @return
	 */
	@Action("planTransferPass")
	public String planTransferPass(){
		//判断该条记录是否已经审核过
				PlanTransfer pt = planTransferService.getByRecordNo(getParam("recordNo"));
				if(pt != null){
					String status = pt.getStatus();
					if(!"WAIT_AUDIT".equals(status)){//记录已经审核过
						this.setResult(true, "该条记录已经审核过", null);
						return NONE;
					}
				}
		Long userId = ((User)this.getRequest().getSession().getAttribute("user")).getId();
		boolean result = planTransferService.saveAduitPass(getParam("recordNo"), userId);
		if(result){
			this.setResult(true, "操作成功!", null);
		}else{
			this.setResult(false, "操作失败!", null);
		}
		return NONE;
	}
	/**
	 * 投资计划债权转让列表审核——重新审核
	 * @return
	 */
	@Action("planTransferReApply")
	public String reApply(){
		Long userId = ((User)this.getRequest().getSession().getAttribute("user")).getId();
		boolean result = planTransferService.saveAduitReApply(getParam("recordNo"), userId);
		if(result){
			this.setResult(true, "操作成功!", null);
		}else{
			this.setResult(false, "操作失败!", null);
		}
		return NONE;
	}
	/**
	 * 查看投资计划未通过原因
	 * @return
	 */
	@Action("viewNoPassReason")
	public String viewNoPassReason(){
		this.setResult(true, null, planTransferService.getNewestAudit(getParam("recordNo")));
		return NONE;
	}
	
	@Action("getProjectDetail")
	public String getProjectDetail(){
		this.setResult(true, null, planTransferService.getLoanClaimByProjectNo(getParam("projectNo")));
		return NONE;
	}
}
